package cn.drenal.capcap.service.impl;

import cn.drenal.capcap.entity.Activity;
import cn.drenal.capcap.entity.ActivityVote;
import cn.drenal.capcap.entity.Artwork;
import cn.drenal.capcap.mapper.ActivityMapper;
import cn.drenal.capcap.mapper.ActivityVoteMapper;
import cn.drenal.capcap.mapper.ArtworkMapper;
import cn.drenal.capcap.service.ActivityVoteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动投票服务实现类
 */
@Service
public class ActivityVoteServiceImpl extends ServiceImpl<ActivityVoteMapper, ActivityVote> implements ActivityVoteService {

    private final ActivityVoteMapper activityVoteMapper;
    private final ActivityMapper activityMapper;
    private final ArtworkMapper artworkMapper;

    public ActivityVoteServiceImpl(ActivityVoteMapper activityVoteMapper, ActivityMapper activityMapper, ArtworkMapper artworkMapper) {
        this.activityVoteMapper = activityVoteMapper;
        this.activityMapper = activityMapper;
        this.artworkMapper = artworkMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean vote(Long userId, Long activityId, Long artworkId) {
        // 检查活动是否存在且处于投票期
        Activity activity = activityMapper.selectById(activityId);
        if (activity == null || activity.getStatus() != 2) {
            return false;
        }

        // 检查作品是否存在且属于该活动
        Artwork artwork = artworkMapper.selectById(artworkId);
        if (artwork == null || !artwork.getActivityId().equals(activityId)) {
            return false;
        }

        // 检查用户是否已对该作品投过票
        if (hasVoted(userId, artworkId)) {
            return false;
        }

        // 检查用户在该活动的投票次数是否已达上限（2次）
        if (getUserVoteCount(userId, activityId) >= 2) {
            return false;
        }

        // 创建投票记录
        ActivityVote vote = new ActivityVote();
        vote.setUserId(userId);
        vote.setActivityId(activityId);
        vote.setArtworkId(artworkId);
        vote.setStatus(0); // 有效状态

        return save(vote);
    }

    @Override
    public int getUserVoteCount(Long userId, Long activityId) {
        LambdaQueryWrapper<ActivityVote> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityVote::getUserId, userId)
                .eq(ActivityVote::getActivityId, activityId)
                .eq(ActivityVote::getStatus, 0); // 只计算有效票

        return Math.toIntExact(activityVoteMapper.selectCount(wrapper));
    }

    @Override
    public boolean hasVoted(Long userId, Long artworkId) {
        LambdaQueryWrapper<ActivityVote> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityVote::getUserId, userId)
                .eq(ActivityVote::getArtworkId, artworkId)
                .eq(ActivityVote::getStatus, 0); // 只检查有效票

        return activityVoteMapper.selectCount(wrapper) > 0;
    }

    @Override
    public int getArtworkVoteCount(Long artworkId) {
        LambdaQueryWrapper<ActivityVote> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityVote::getArtworkId, artworkId)
                .eq(ActivityVote::getStatus, 0); // 只计算有效票

        return Math.toIntExact(activityVoteMapper.selectCount(wrapper));
    }
}